$(function(){

  //  顶部广告
  $('.ad-cancel').on('click',function(){
    $('.top-ad').hide()
  })

  $('.link-app').on('click',function(){
    $('.mask-ad').show()
    setTimeout(function(){
      $('.mask-link').hide()
      $('.mask-link-load').show()
    },3000)
  })
  //  顶部广告关闭
  $('.continue').on('click',function(){
    $('.mask-ad').hide()
  })


  //  功能列表换页
  classifyMove()
  function classifyMove(){
    var focus = document.querySelector('.classify-box')
    var ul = focus.children[0]
    var w = focus.offsetWidth
    var index = 0
    var startX=0
    var moveX=0
    var flag =false
    ul.addEventListener('touchstart', function (e) {
      startX = e.targetTouches[0].pageX
    })

    ul.addEventListener('touchmove',function (e) {
      moveX = e.targetTouches[0].pageX - startX
      flag = true
      e.preventDefault()
    })

    ul.addEventListener('touchend',function (e) {
      if (flag) {
        flag = false
        if (Math.abs(moveX) > 50) {
          $('#classify-circle li').removeClass('current')
            if (moveX > 0) {
                index=0
                $('#classify-circle li').eq(0).addClass('current')
            } else {
                index=1
                $('#classify-circle li').eq(1).addClass('current')
            }
            var translatex = -index * w -4
            if(index===0||index===1) {
              ul.style.transition = 'all .3s'
              ul.style.transform = 'translateX(' + translatex + 'px)'
            }
        } else {
            var translatex = -index * w
            ul.style.transition = 'all .1s'
            ul.style.transform = 'translateX(' + translatex + 'px)'
        }
      }
    })

  } 


  //  秒杀倒计时
  var nowTime =0
  var inputTime = 0
  var times = 0
  var h = 0
  var m = 0
  var s = 0
  var downTime
  setInterval(function(){
    //获取剩余秒数
    function countDown(time) {
      nowTime = +new Date();
      inputTime = +new Date(time);
      times = (inputTime - nowTime) / 1000;
      h = parseInt(times / 60 / 60 % 24); //时
      h = h < 10 ? '0' + h : h;
      m = parseInt(times / 60 % 60); // 分
      m = m < 10 ? '0' + m : m;
      s = parseInt(times % 60); // 当前的秒
      s = s < 10 ? '0' + s : s;
      return [h,m,s]
    }
    //  给页面赋值
    seckillTime()
    function seckillTime(){
      downTime= countDown('2022-12-31 00:00:00')
      $('.seckill-time span').eq(0).html(downTime[0])
      $('.seckill-time span').eq(1).html(downTime[1])
      $('.seckill-time span').eq(2).html(downTime[2])
    }
  }, 1000);


  //  返回顶部
  $(window).scroll(function(){
    if($(window).scrollTop()>= $('.main').offset().top) {
      $('#backTop').show()
    } else {
      $('#backTop').hide()
    }
  })
  //  点击返回顶部
  $('.backTop').on('click',function (){
    window.scroll({
        top: 0,
        behavior: 'smooth',
    })
  })


  //  头部banner区域轮播
  bannerMove()
  function bannerMove(){
    var bannerUl = $('.hd-banner ul')[0]
    var bannerOl = $('.hd-banner ol')[0]
    var w = $('.hd-banner')[0].offsetWidth
    var timer
    var index =0
    var translatex=0
    var startX=0
    var moveX=0
    var flag = false

    //  轮播定时效果
    timerMove()
    function timerMove(){
      timer = setInterval(function() {
        index++
        translatex = -index * w
        bannerUl.style.transition = 'all .3s'
        bannerUl.style.transform = 'translate(' + translatex + 'px)'
      }, 2500);
    }

    bannerUl.addEventListener('transitionend', function () {
      if (index >= 8) {
          index = 0
          bannerUl.style.transition = 'none'
          translatex = -index * w
          bannerUl.style.transform = 'translateX(' + translatex + 'px)'
      } else if (index < 0) {
          index = 7
          bannerUl.style.transition = 'none'
          translatex = -index * w
          bannerUl.style.transform = 'translateX(' + translatex + 'px)'
      }

      bannerOl.querySelector('.current').classList.remove('current')
      bannerOl.children[index].classList.add('current')
    })

    //  模拟点击移动
    $(bannerUl).on('touchstart',function(e){
      startX = e.targetTouches[0].pageX
      clearInterval(timer)
    })

    $(bannerUl).on('touchmove',function(e){
      moveX = e.targetTouches[0].pageX - startX
      translatex = -index * w + moveX
      this.style.transition = 'none'
      this.style.transform = 'translateX(' + translatex + 'px)'
      flag = true
    })

    $(bannerUl).on('touchend',function(){
      if(flag) {
        flag = false
        if (Math.abs(moveX) > 40) {
          if (moveX > 0) {
              index--
          } else {
              index++
          }
          translatex = -index * w
          this.style.transition = 'all .3s'
          this.style.transform = 'translateX(' + translatex + 'px)'
        } else {
          translatex = -index * w
          this.style.transition = 'all .1s'
          this.style.transform = 'translateX(' + translatex + 'px)'
        }  
      }
      timerMove()
    })

  }


  //  页面初始广告关闭
  $('.big-cancel').on('click',function(){
    $('.big-ad').hide()
  })
  setTimeout(function() {
    $('.big-ad').hide()
  }, 5000);


  //  底部小广告
  $('.small-cancel').on('click',function(){
    $(this).parent().hide()
  })
  adTxtMove()
  function adTxtMove(){
    var index =0
    var h = $('#ad-txt li')[0].offsetHeight
    var translatey
    setInterval(function() {
      index++
      translatey = -index * h
      $('#ad-txt')[0].style.transition = 'all .8s'
      $('#ad-txt')[0].style.transform = 'translateY(' + translatey + 'px)'
      
    }, 3000);
    $('#ad-txt').on('transitionend',function(){
      if(index>=2){
        index=0
        $('#ad-txt')[0].style.transition = 'none'
        translatey = -index * h
        $('#ad-txt')[0].style.transform = 'translateY(' + translatey + 'px)'
      }
    })
  }


})